package com.zy.controller;

import com.zy.entity.DepartmentPosition;
import com.zy.entity.JsonResult;
import com.zy.mapper.DepartmentPositionMapper;
import com.zy.tools.RedisTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/app")
public class DepartmentPositionController {
    @Autowired
    DepartmentPositionMapper departmentPositionMapper;
    @Autowired
    RedisTool redisTool;


    /**
     * 获取职位list
     *
     * @return com.zy.entity.JsonResult
     * @author 小宁
     * @date 2021-04-18 22:15
     */
    @GetMapping("/getDepartmentPositionList")
    @ResponseBody
    public JsonResult getDepartmentPositionList() {

        List<DepartmentPosition> departmentPositionList = null;
        Map<String, List<DepartmentPosition>> groups = null;
        if (redisTool.hasKey("DepartmentPositionList")) {
            groups = (Map<String, List<DepartmentPosition>>) redisTool.get("DepartmentPositionList");
        } else {
            departmentPositionList = departmentPositionMapper.getDepartmentPositionList();
            groups = departmentPositionList.stream().collect(Collectors.groupingBy(DepartmentPosition::getDepartmentName, Collectors.toList()));
            redisTool.set("DepartmentPositionList", groups, 60 * 60);

        }

        return new JsonResult("200", "", groups);
    }
}
